package spring.cloud.study.filter;

import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;

/**
 * @ClassName:LogGatewayFilter   
 * @Description:配置gateway全局自定义过滤器
 * @author:Calvin.W
 * @date:2021年12月22日 下午2:37:49
 */
@Component
@Slf4j
public class LogGatewayFilter implements GlobalFilter,Ordered {

	//加载过滤器顺序，数字越小越优先
	@Override
	public int getOrder() {
		return 0;
	}

	
	/**
	 * 
	 */
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		log.info("Gateway Global Filter...");
		String userName = exchange.getRequest().getQueryParams().getFirst("userName");
		if(StringUtils.isBlank(userName)) {
			log.error("用户名为null");
			exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
			return exchange.getResponse().setComplete();
		}
		return chain.filter(exchange);
	}
	

}
